# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:tuan_quanwen1_2.py
@Time:2024/8/14 8:50

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""


def read_file_and_create_dict(filename, k64):
    tuan_dict = {}
    current_key_index = 0
    value = []

    with open(filename, 'r', encoding='utf-8') as file:
        for line in file:
            clean_line = line.strip()
            if "彖传原文" in clean_line:
                # 检查是否超出k64的索引范围
                if current_key_index < len(k64):
                    if value:
                        tuan_dict[k64[current_key_index - 1]] = value
                    value = []  # 重置value列表
                    current_key_index += 1
                else:
                    # 如果索引超出范围，但仍有"彖传原文"，可能是文件结构问题，可以记录或忽略
                    print(f"Warning: '彖传原文' found beyond k64 index range. Skipping.")
            else:
                if clean_line:
                    value.append(clean_line)

                # 循环结束后，添加最后一个value列表到字典中（如果存在）
        if value and current_key_index <= len(k64):
            tuan_dict[k64[current_key_index - 1]] = value

    return tuan_dict


# 64卦的列表
k64 = [
    '乾', '坤', '屯', '蒙', '需', '讼', '师', '比', '小畜', '履', '泰', '否',
    '同人', '大有', '谦', '豫', '随', '蛊', '临', '观', '噬嗑', '贲', '剥',
    '复', '无妄', '大畜', '颐', '大过', '坎', '离', '咸', '恒', '遁', '大壮',
    '晋', '明夷', '家人', '睽', '蹇', '解', '损', '益', '夬', '姤', '萃',
    '升', '困', '井', '革', '鼎', '震', '艮', '渐', '归妹', '丰', '旅', '巽', '兑',
    '涣', '节', '中孚', '小过', '既济', '未济'
]

filename = 'yi/tuan_quanwen.txt'
tuan_dict = read_file_and_create_dict(filename, k64)

print(tuan_dict)

# 打印字典中的所有项
for key in tuan_dict:
    print(f"{key}:")
    for line in tuan_dict[key]:
        print(f"{line}")